window.onload = function() {
	document.getElementById("form").onsubmit = function() {
		return checkUsername() && checkPassword() && checkEmail() && checktel();
	};
	document.getElementById("username").onblur = checkUsername;
	document.getElementById("password").onblur = checkPassword;
	document.getElementById("email").onblur = checkEmail;
	document.getElementById("tel").onblur = checktel;
	document.getElementById("fasong").onclick=fasong;
};

function checkUsername() {
	var username = document.getElementById("username").value;
	var reg_username = /^[a-zA-Z0-9][a-zA-Z0-9._-]{2,16}[a-zA-Z0-9]$/;
	//1、由字母、数字、下划线、点、减号组成 2、只能以数字、字母开头或结尾，且长度为4~18
	var flag = reg_username.test(username);
	var s_username = document.getElementById("s_username");
	if (!flag) {
		s_username.innerHTML = "用户名格式有误";
		s_username.style.color = "red"
	} else {
		s_username.innerHTML = "用户名格式正确";
		s_username.style.color = "green"
	}
	return flag;
}

function checkPassword() {
	var password = document.getElementById("password").value;
	var reg_password = /[a-zA-Z0-9]{6,10}$/;
	//密码只能是6位以上的字符
	var flag = reg_password.test(password);
	var s_password = document.getElementById("s_password");
	if (!flag) {
		s_password.innerHTML = "密码格式有误";
		s_password.style.color = "red"
	} else {
		s_password.innerHTML = "密码格式正确";
		s_password.style.color = "green"
	}
	return flag;
}

function checkEmail() {
	var email = document.getElementById("email").value;
	var s_email = document.getElementById("s_email");
	if (email == "") {
		s_email.innerHTML = "邮箱不能为空！";
		s_email.style.color = "red";
	} else {
		s_email.innerHTML = "邮箱已填写！";
		s_email.style.color = "green";
	}
}

function checktel(){
	var btn=document.getElementById('fasong');
	var time=10;
	btn.addEventListener('click',function(){
		btn.disabled=true;
		var timer=setInterval(function(){
			if(time==0){
				clearInterval(timer);
				btn.disabled=false;
				btn.innerHTML='发送';
				time=10;
			}else{
				btn.innerHTML=time+'秒禁用...';
				time--;
			}
		},1000);
	})
}
